package cn.com.yinhex.as.util;

import java.util.ArrayList;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Remark;
import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.NodeVisitor;

public class HTMLFormator {
	public static String format(String body) {
		try {
			Parser parser = Parser.createParser(body, "GBK");
			NodeVisitor visitor = new FormatVisitor();
			parser.visitAllNodesWith(visitor);
			return visitor.toString();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return body;
		}
	}
}

class FormatVisitor extends NodeVisitor {
	StringBuffer out = null;

	static String bad_tagname = "|table|tbody|td|font|span|h2|h3|center|th|sohu|form|input|select|textarea|BLOCKQUOTE|nobr|dd|dt|iframe|strong|b|?xml|o:p|body|html|head|meta|noscript|nobr|frameset|"
			.toLowerCase();

	static String bad_tagname_br = "|code|div|tr|th|br|p|".toLowerCase();

	static String bad_tag = "|script|style|";

	boolean start_text = false;

	boolean write_mm = true;

	static String NEXT_LINE = "<br><br>";

	public FormatVisitor() {
		out = new StringBuffer();
	}

	public void visitTag(Tag tag) {
		if (tag.getTagName() == null) {
			return;
		}
		System.out.println(tag.getTagName() + ": " + start_text);
		if (bad_tagname_br.contains("|" + tag.getTagName().toLowerCase() + "|")) {
			System.out.println("bad_tagname_br: " + tag.getTagName().toLowerCase());
			if (start_text && write_mm) {
				out.append(NEXT_LINE + "\n");
				write_mm = false;
			}
		} else if (bad_tagname.contains("|" + tag.getTagName().toLowerCase() + "|")) {
			System.out.println("bad_tagname: " + tag.getTagName().toLowerCase());
		} else if (bad_tag.contains("|" + tag.getTagName().toLowerCase() + "|")) {
			System.out.println("bad_tag: " + tag.getTagName().toLowerCase());
			tag.setChildren(null);
			tag.setEndTag(null);
		} else if (tag.getTagName().toLowerCase().equals("hr") && tag.getAttribute("color") != null && tag.getAttribute("color").equals("#ff0123")) {
			tag.setChildren(null);
			tag.setEndTag(null);
		} else if (!tag.isEndTag() && tag.getTagName().toLowerCase().equals("img")) {
			out.append("<img src=\"" + tag.getAttribute("src") + "\">");
			out.append(NEXT_LINE + "\n");
			start_text = true;
			write_mm = true;
		} else if (!tag.isEndTag() && tag.getTagName().toLowerCase().equals("a")) {
			if (tag.getAttribute("href") != null && tag.getAttribute("href").startsWith("http://")) {
				out.append("<a href=\"" + tag.getAttribute("href") + "\" target=\"_blank\">");
			}
			start_text = true;
			write_mm = true;
		} else {
			out.append("<" + tag.getText() + ">");
			start_text = true;
			write_mm = true;
		}
	}

	public void visitEndTag(Tag tag) {
		if (tag.getTagName() == null) {
			return;
		}
		if (bad_tagname_br.contains("|" + tag.getTagName().toLowerCase() + "|")) {
			System.out.println("bad_tagname_br: " + tag.getTagName().toLowerCase());
			if (start_text && write_mm) {
				out.append(NEXT_LINE + "\n");
				write_mm = false;
			}
		} else if (bad_tagname.contains("|" + tag.getTagName().toLowerCase() + "|")) {
			System.out.println("bad_tagname: " + tag.getTagName().toLowerCase());
		} else {
			if (start_text) {
				write_mm = true;
				out.append("<" + tag.getText() + ">");
			}
		}
	}

	public void visitRemarkNode(Remark tag) {
		return;
	}

	public void visitStringNode(Text tag) {
		String text = tag.toHtml().replaceAll("\\s+", " ").replaceAll("��", "");
		if (StringUtil.isFine(text)) {
			out.append(text);
			start_text = true;
			write_mm = true;
		}
	}

	public void finishedParsing() {
		if (!write_mm) {
			out = new StringBuffer(out.substring(0, out.lastIndexOf(NEXT_LINE)));
		}
	}

	public String toString() {
		return out.toString();
	}

}
